'use strict';
const { get } = require('lodash');
const _assembleAllParams = ctx => {
  return {
    body: ctx.request.body,
    query: ctx.request.query,
    path: ctx.params,
    header: ctx.request.header,
    files: ctx.req.files && ctx.req.files.file,
    fields: ctx.req.fields,
  };
};

module.exports = {
  params(key) {
    const paramsValue = _assembleAllParams(this);
    return get(paramsValue, key) || paramsValue;
  },
  get username() {
    const token = this.request.header.token;
    const tokenCache = token ? this.app.jwt.verify(token, this.app.config.jwt.secret) : undefined;
    return tokenCache ? tokenCache.username : undefined;
  },
  get userId() {
    const token = this.request.header.token;
    const tokenCache = token ? this.app.jwt.verify(token, this.app.config.jwt.secret) : undefined;
    return tokenCache ? tokenCache.id : undefined;
  },
};

